/**
 * Copyright(C) 2008 Matthias Braunhofer
 * All rights reserved
 *
 * Created on: 07.09.2008
 */

package it.cums.ui.printing;

import java.util.Date;
import java.util.List;

import it.cums.core.model.Customer;
import it.cums.core.model.Invoice;
import it.cums.core.model.Order;
import it.cums.core.model.OrderedProduct;
import it.cums.core.model.Product;
import it.cums.core.model.Supplier;
import it.cums.core.util.Converter;
import it.cums.core.util.InvoiceManager;
import it.cums.core.util.OrderedProductManager;
import it.cums.ui.localization.Messages;

import net.sf.paperclips.DefaultGridLook;
import net.sf.paperclips.GridLook;
import net.sf.paperclips.GridPrint;
import net.sf.paperclips.LinePrint;
import net.sf.paperclips.PageDecoration;
import net.sf.paperclips.PageNumber;
import net.sf.paperclips.PageNumberPrint;
import net.sf.paperclips.PagePrint;
import net.sf.paperclips.Print;
import net.sf.paperclips.SimplePageDecoration;
import net.sf.paperclips.TextPrint;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;

public class PrintingDesign {
	
    public static PagePrint createPrint(Object object) {
    	DefaultGridLook  look = new DefaultGridLook();
    	look.setCellSpacing(5, 2);
        GridPrint grid = new GridPrint("p:g, d:g", look); //$NON-NLS-1$
        final FontData bold = new FontData("Arial", 10, SWT.BOLD); //$NON-NLS-1$
        final FontData normal = new FontData("Arial", 10, SWT.NORMAL); //$NON-NLS-1$
        if (object instanceof Customer) {
        	prepareCustomerData((Customer)object, grid, bold, normal);
        } else if (object instanceof Product) {
        	prepareProductData((Product)object, grid, bold, normal);
        } else if (object instanceof Supplier) {
        	prepareSupplierData((Supplier)object, grid, bold, normal);
        } else if (object instanceof Order) {
        	prepareOrderData((Order)object, grid, bold, normal);
        } else if (object instanceof Invoice) {
        	prepareInvoiceData((Invoice)object, grid, bold, normal);
        }
        PagePrint page = new PagePrint(grid);
        page.setHeader(new SimplePageDecoration(new TextPrint(object.toString(), SWT.CENTER)));
        PageDecoration footer = new PageDecoration() {
            private final GridLook footerLook = new DefaultGridLook(5, 2);
            
            public Print createPrint(PageNumber pageNumber) {
              GridPrint grid = new GridPrint("d:g, d", this.footerLook); //$NON-NLS-1$
              grid.add(new LinePrint(SWT.HORIZONTAL), GridPrint.REMAINDER);
              PageNumberPrint pageNumberPrint = new PageNumberPrint(pageNumber, normal);
              pageNumberPrint.setAlign(SWT.CENTER);
              grid.add(pageNumberPrint);
              return grid;
            }
          };
        page.setFooter(footer);
        page.setHeaderGap(5);
        page.setFooterGap(2);

        return page;
    }
    
    private static void prepareInvoiceData(Invoice invoice, GridPrint grid, FontData bold, FontData normal) {
    	//header
    	grid.add(new TextPrint(Messages.PrintingDesign_4, bold), GridPrint.REMAINDER);
    	
    	//invoice number
    	grid.add(new TextPrint(Messages.PrintingDesign_5, bold));
    	grid.add(new TextPrint(String.valueOf(invoice.getId()), normal));
    	
    	//creation date
    	grid.add(new TextPrint(Messages.PrintingDesign_6, bold));
    	grid.add(new TextPrint(Converter.convertToString(new Date()), normal));
    	
    	//header
    	grid.add(new TextPrint(Messages.PrintingDesign_7, bold), GridPrint.REMAINDER);
    	
    	//name
    	grid.add(new TextPrint(Messages.PrintingDesign_8, bold));
    	grid.add(new TextPrint(invoice.getOrder().getCustomer().getName(), normal));
    	
    	//address
    	grid.add(new TextPrint(Messages.PrintingDesign_9, bold));
    	grid.add(new TextPrint(invoice.getOrder().getCustomer().getAddress(), normal));
    	
    	//location
    	grid.add(new TextPrint(Messages.PrintingDesign_10, bold));
    	grid.add(new TextPrint(invoice.getOrder().getCustomer().getCap() + " " + invoice.getOrder().getCustomer().getLocation() + " (" + invoice.getOrder().getCustomer().getCountry().getDescription().toUpperCase() + ")", normal)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	
    	//tax number
    	grid.add(new TextPrint(Messages.PrintingDesign_14, bold));
    	grid.add(new TextPrint(invoice.getOrder().getCustomer().getTaxNumber(), normal));
    	
    	//line
    	grid.add(new TextPrint("\n\n ", normal), GridPrint.REMAINDER); //$NON-NLS-1$
    	grid.add(new LinePrint(SWT.HORIZONTAL), GridPrint.REMAINDER);
    	
    	//total amount
    	FontData largeBold = new FontData("Arial", 16, SWT.BOLD); //$NON-NLS-1$
    	grid.add(new TextPrint(Messages.PrintingDesign_17, bold));
        grid.add(new TextPrint(invoice.getAmount() + " Euro", largeBold)); //$NON-NLS-1$
        
        //due date
        grid.add(new TextPrint(Messages.PrintingDesign_19, bold));
        grid.add(new TextPrint(Converter.convertToString(invoice.getDueDate()), largeBold));
        
        //condition / type
        grid.add(new TextPrint(Messages.PrintingDesign_20, bold));
        grid.add(new TextPrint(invoice.getType() + " / " + invoice.getCondition(), largeBold)); //$NON-NLS-1$
        
        //line
        grid.add(new LinePrint(SWT.HORIZONTAL), GridPrint.REMAINDER);
        grid.add(new TextPrint("\n ", normal), GridPrint.REMAINDER); //$NON-NLS-1$
        
        //header
		grid.add(new TextPrint(Messages.PrintingDesign_23, bold), GridPrint.REMAINDER);
    	
		//order date
        grid.add(new TextPrint(Messages.PrintingDesign_24, bold));
        grid.add(new TextPrint(Converter.convertToString(invoice.getOrder().getOrderDate()), normal));
        
        //total price
        grid.add(new TextPrint(Messages.PrintingDesign_25, bold));
        grid.add(new TextPrint(String.valueOf(invoice.getOrder().getTotalPrice()), normal));
        
        //discount
        grid.add(new TextPrint(Messages.PrintingDesign_26, bold));
        grid.add(new TextPrint(String.valueOf(invoice.getOrder().getDiscount()), normal));
        
        //end price
        grid.add(new TextPrint(Messages.PrintingDesign_27, bold));
        grid.add(new TextPrint(String.valueOf(invoice.getOrder().getEndPrice()), normal));
        
      	//header
		grid.add(new TextPrint(Messages.PrintingDesign_28, bold), GridPrint.REMAINDER);
		
		DefaultGridLook look = new DefaultGridLook();
        look.setCellSpacing(5, 2);
		GridPrint gridProductsPrint = new GridPrint("d:g, d:g, d:g, d:g, d:g, d:g", look); //$NON-NLS-1$
		
		//column headers
		gridProductsPrint.add(new TextPrint(Messages.PrintingDesign_30, bold));
		gridProductsPrint.add(new TextPrint(Messages.PrintingDesign_31, bold));
		gridProductsPrint.add(new TextPrint(Messages.PrintingDesign_32, bold));
		gridProductsPrint.add(new TextPrint(Messages.PrintingDesign_33, bold));
		gridProductsPrint.add(new TextPrint(Messages.PrintingDesign_34, bold));
		gridProductsPrint.add(new TextPrint(Messages.PrintingDesign_35, bold));
		
		//ordered products
		List<OrderedProduct> products = OrderedProductManager.getInstance().listOrderedProducts(invoice.getOrder());
		for (OrderedProduct orderedProduct : products) {
			gridProductsPrint.add(new TextPrint(orderedProduct.getProduct(), normal));
			gridProductsPrint.add(new TextPrint(String.valueOf(orderedProduct.getPrice()), normal));
			gridProductsPrint.add(new TextPrint(String.valueOf(orderedProduct.getQuantity()), normal));
			gridProductsPrint.add(new TextPrint(String.valueOf(orderedProduct.getTotalPrice()), normal));
			gridProductsPrint.add(new TextPrint(String.valueOf(orderedProduct.getDiscount()), normal));
			gridProductsPrint.add(new TextPrint(String.valueOf(orderedProduct.getEndPrice()), normal));
		}   
		
		grid.add(gridProductsPrint, 2);
    }

	private static void prepareOrderData(Order order, GridPrint grid, FontData bold, FontData normal) {
    	//header
		grid.add(new TextPrint(Messages.PrintingDesign_36, bold), GridPrint.REMAINDER);
		
        //customer
        grid.add(new TextPrint(Messages.PrintingDesign_37, bold));
        grid.add(new TextPrint(order.getCustomer().getName(), normal));
        
        //priority
        grid.add(new TextPrint(Messages.PrintingDesign_38, bold));
        grid.add(new TextPrint(order.getPriority(), normal));
        
        //order date
        grid.add(new TextPrint(Messages.PrintingDesign_39, bold));
        grid.add(new TextPrint(Converter.convertToString(order.getOrderDate()), normal));
        
        //delivery date
        grid.add(new TextPrint(Messages.PrintingDesign_40, bold));
        grid.add(new TextPrint(Converter.convertToString(order.getDeliveryDate()), normal));
    
        //total price
        grid.add(new TextPrint(Messages.PrintingDesign_41, bold));
        grid.add(new TextPrint(String.valueOf(order.getTotalPrice()), normal));
        
        //discount
        grid.add(new TextPrint(Messages.PrintingDesign_42, bold));
        grid.add(new TextPrint(String.valueOf(order.getDiscount()), normal));
        
        //end price
        grid.add(new TextPrint(Messages.PrintingDesign_43, bold));
        grid.add(new TextPrint(String.valueOf(order.getEndPrice()), normal));
        
        //header
		grid.add(new TextPrint(Messages.PrintingDesign_44, bold), GridPrint.REMAINDER);
		
		DefaultGridLook look = new DefaultGridLook();
        look.setCellSpacing(5, 2);
		GridPrint gridProductsPrint = new GridPrint("d:g, d:g, d:g, d:g, d:g, d:g", look); //$NON-NLS-1$
		
		//column headers
		gridProductsPrint.add(new TextPrint(Messages.PrintingDesign_46, bold));
		gridProductsPrint.add(new TextPrint(Messages.PrintingDesign_47, bold));
		gridProductsPrint.add(new TextPrint(Messages.PrintingDesign_48, bold));
		gridProductsPrint.add(new TextPrint(Messages.PrintingDesign_49, bold));
		gridProductsPrint.add(new TextPrint(Messages.PrintingDesign_50, bold));
		gridProductsPrint.add(new TextPrint(Messages.PrintingDesign_51, bold));
		
		//ordered products
		List<OrderedProduct> products = OrderedProductManager.getInstance().listOrderedProducts(order);
		for (OrderedProduct orderedProduct : products) {
			gridProductsPrint.add(new TextPrint(orderedProduct.getProduct(), normal));
			gridProductsPrint.add(new TextPrint(String.valueOf(orderedProduct.getPrice()), normal));
			gridProductsPrint.add(new TextPrint(String.valueOf(orderedProduct.getQuantity()), normal));
			gridProductsPrint.add(new TextPrint(String.valueOf(orderedProduct.getTotalPrice()), normal));
			gridProductsPrint.add(new TextPrint(String.valueOf(orderedProduct.getDiscount()), normal));
			gridProductsPrint.add(new TextPrint(String.valueOf(orderedProduct.getEndPrice()), normal));
		}
	
		grid.add(gridProductsPrint, 2);
		
		//header
		grid.add(new TextPrint(Messages.PrintingDesign_52, bold), GridPrint.REMAINDER);
		
		GridPrint gridInvoicesPrint = new GridPrint("d:g, d:g, d:g, d:g, d:g, d:g", look); //$NON-NLS-1$
		
		//column headers
		gridInvoicesPrint.add(new TextPrint(Messages.PrintingDesign_54, bold));
		gridInvoicesPrint.add(new TextPrint(Messages.PrintingDesign_55, bold));
		gridInvoicesPrint.add(new TextPrint(Messages.PrintingDesign_56, bold));
		gridInvoicesPrint.add(new TextPrint(Messages.PrintingDesign_57, bold));
		gridInvoicesPrint.add(new TextPrint(Messages.PrintingDesign_58, bold));
		gridInvoicesPrint.add(new TextPrint(Messages.PrintingDesign_59, bold));
		
		//invoices
		List<Invoice> invoices = InvoiceManager.getInstance().listInvoices(order);
		for (Invoice invoice : invoices) {
			gridInvoicesPrint.add(new TextPrint(Converter.convertToString(invoice.getDueDate()), normal));
			gridInvoicesPrint.add(new TextPrint(String.valueOf(invoice.getAmount()), normal));
			gridInvoicesPrint.add(new TextPrint(invoice.getCondition(), normal));
			gridInvoicesPrint.add(new TextPrint(invoice.getType(), normal));
			gridInvoicesPrint.add(new TextPrint(invoice.getStatus(), normal));
			gridInvoicesPrint.add(new TextPrint(Converter.convertToString(invoice.getPaymentDate()), normal));
		}
	
		grid.add(gridInvoicesPrint, 2);
    }

	private static void prepareSupplierData(Supplier supplier, GridPrint grid, FontData bold, FontData normal) {
		//header
		grid.add(new TextPrint(Messages.PrintingDesign_60, bold), GridPrint.REMAINDER);
		
        //name
        grid.add(new TextPrint(Messages.PrintingDesign_61, bold));
        grid.add(new TextPrint(supplier.getName(), normal));
        
        //tax number
        grid.add(new TextPrint(Messages.PrintingDesign_62, bold));
        grid.add(new TextPrint(supplier.getTaxNumber(), normal));
        
        //reference person
        grid.add(new TextPrint(Messages.PrintingDesign_63, bold));
        grid.add(new TextPrint(supplier.getReferencePerson(), normal));
        
      	//header
		grid.add(new TextPrint(Messages.PrintingDesign_64, bold), GridPrint.REMAINDER);
		
		//address
		grid.add(new TextPrint(Messages.PrintingDesign_65, bold));
        grid.add(new TextPrint(supplier.getAddress(), normal));
        
        //CAP
        grid.add(new TextPrint(Messages.PrintingDesign_66, bold));
        grid.add(new TextPrint(supplier.getCap(), normal));
        
        //location
        grid.add(new TextPrint(Messages.PrintingDesign_67, bold));
        grid.add(new TextPrint(supplier.getLocation(), normal));
		
        //country
        grid.add(new TextPrint(Messages.PrintingDesign_68, bold));
        grid.add(new TextPrint(supplier.getCountry().getDescription(), normal));
        
        //phone
        grid.add(new TextPrint(Messages.PrintingDesign_69, bold));
        grid.add(new TextPrint(supplier.getPhone(), normal));
        
        //mobile phone
        grid.add(new TextPrint(Messages.PrintingDesign_70, bold));
        grid.add(new TextPrint(supplier.getMobilePhone(), normal));
        
        //email
        grid.add(new TextPrint(Messages.PrintingDesign_71, bold));
        grid.add(new TextPrint(supplier.getEmail(), normal));
        
        //website
        grid.add(new TextPrint(Messages.PrintingDesign_72, bold));
        grid.add(new TextPrint(supplier.getWebSite(), normal));
        
      	//header
		grid.add(new TextPrint(Messages.PrintingDesign_73, bold), GridPrint.REMAINDER);
		grid.add(new TextPrint(supplier.getAccountDetails(), normal), GridPrint.REMAINDER);
	    
        //header
		grid.add(new TextPrint(Messages.PrintingDesign_74, bold), GridPrint.REMAINDER);
		grid.add(new TextPrint(supplier.getNotes(), normal), GridPrint.REMAINDER);
	}

	private static void prepareCustomerData(Customer customer, GridPrint grid, FontData bold, FontData normal) {
		//header
		grid.add(new TextPrint(Messages.PrintingDesign_75, bold), GridPrint.REMAINDER);
		
        //name
        grid.add(new TextPrint(Messages.PrintingDesign_76, bold));
        grid.add(new TextPrint(customer.getName(), normal));
        
        //tax number
        grid.add(new TextPrint(Messages.PrintingDesign_77, bold));
        grid.add(new TextPrint(customer.getTaxNumber(), normal));
        
        //reference person
        grid.add(new TextPrint(Messages.PrintingDesign_78, bold));
        grid.add(new TextPrint(customer.getReferencePerson(), normal));
        
        //header
		grid.add(new TextPrint(Messages.PrintingDesign_79, bold), GridPrint.REMAINDER);
		
		//address
		grid.add(new TextPrint(Messages.PrintingDesign_80, bold));
        grid.add(new TextPrint(customer.getAddress(), normal));
        
        //CAP
        grid.add(new TextPrint(Messages.PrintingDesign_81, bold));
        grid.add(new TextPrint(customer.getCap(), normal));
        
        //location
        grid.add(new TextPrint(Messages.PrintingDesign_82, bold));
        grid.add(new TextPrint(customer.getLocation(), normal));
		
        //country
        grid.add(new TextPrint(Messages.PrintingDesign_83, bold));
        grid.add(new TextPrint(customer.getCountry().getDescription(), normal));
        
        //phone
        grid.add(new TextPrint(Messages.PrintingDesign_84, bold));
        grid.add(new TextPrint(customer.getPhone(), normal));
        
        //mobile phone
        grid.add(new TextPrint(Messages.PrintingDesign_85, bold));
        grid.add(new TextPrint(customer.getMobilePhone(), normal));
        
        //email
        grid.add(new TextPrint(Messages.PrintingDesign_86, bold));
        grid.add(new TextPrint(customer.getEmail(), normal));
        
        //website
        grid.add(new TextPrint(Messages.PrintingDesign_87, bold));
        grid.add(new TextPrint(customer.getWebSite(), normal));
        
      	//header
		grid.add(new TextPrint(Messages.PrintingDesign_88, bold), GridPrint.REMAINDER);
		boolean noDeliveryInformation = customer.getDeliveryCountry() == null;
		
		//delivery address
		grid.add(new TextPrint(Messages.PrintingDesign_89, bold));
	    grid.add(new TextPrint(noDeliveryInformation ? "" : customer.getDeliveryAddress(), normal)); //$NON-NLS-1$
	    
	    //CAP
	    grid.add(new TextPrint(Messages.PrintingDesign_91, bold));
        grid.add(new TextPrint(noDeliveryInformation ? "" : customer.getDeliveryCap(), normal)); //$NON-NLS-1$
	
        //location
        grid.add(new TextPrint(Messages.PrintingDesign_93, bold));
        grid.add(new TextPrint(noDeliveryInformation ? "" : customer.getDeliveryLocation(), normal)); //$NON-NLS-1$
	
        //country
        grid.add(new TextPrint(Messages.PrintingDesign_95, bold));
        grid.add(new TextPrint(noDeliveryInformation ? "" : customer.getDeliveryCountry().getDescription(), normal)); //$NON-NLS-1$
	
        //header
		grid.add(new TextPrint(Messages.PrintingDesign_97, bold), GridPrint.REMAINDER);
		grid.add(new TextPrint(customer.getNotes(), normal), GridPrint.REMAINDER);
	}

	private static void prepareProductData(Product product, GridPrint grid, FontData bold, FontData normal) {
		//header
		grid.add(new TextPrint(Messages.PrintingDesign_98, bold), GridPrint.REMAINDER);
		
        //name
        grid.add(new TextPrint(Messages.PrintingDesign_99, bold));
        grid.add(new TextPrint(product.getName(), normal));
        
        //manufacturer
        grid.add(new TextPrint(Messages.PrintingDesign_100, bold));
        grid.add(new TextPrint(product.getManufacturer(), normal));
        
        //available quantity
        grid.add(new TextPrint(Messages.PrintingDesign_101, bold));
        grid.add(new TextPrint((product.getAvailableQuantity() == -1) ? "" : String.valueOf(product.getAvailableQuantity()), normal)); //$NON-NLS-1$
        
        //purchasing price
        grid.add(new TextPrint(Messages.PrintingDesign_103, bold));
        grid.add(new TextPrint((product.getPurchasingPrice() == -1) ? "" : String.valueOf(product.getPurchasingPrice()), normal)); //$NON-NLS-1$
      
		//iva
        grid.add(new TextPrint(Messages.PrintingDesign_105, bold));
        grid.add(new TextPrint(String.valueOf(product.getIva()), normal));
        
        //sales price
        grid.add(new TextPrint(Messages.PrintingDesign_106, bold));
        grid.add(new TextPrint(String.valueOf(product.getSalesPrice()), normal));
        
        //additional notes
        grid.add(new TextPrint(Messages.PrintingDesign_107, bold));
        grid.add(new TextPrint(product.getNotes(), normal));
    }
}
